package com.ruoyi.web.controller.villagers;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.villagers.domain.VigHouse;
import com.ruoyi.villagers.service.IVigHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/villagers/house")
public class VigHouseController extends BaseController {

    @Autowired
    private IVigHouseService iVigHouseService;


    /**
     * 获取建房列表信息
     */

    @PreAuthorize("@ss.hasPermi('villagers:house:list')")
    @GetMapping("/list")
    public TableDataInfo list(VigHouse house){
        startPage();
        List<VigHouse> list = iVigHouseService.selectHouseList(house);
        return getDataTable(list);
    }

    /**
     * 新增建房信息
     */
    @PreAuthorize("@ss.hasPermi('villagers:house:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody VigHouse house)
    {

        house.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iVigHouseService.insertHouse(house));
    }

    /**
     * 修改建房号
     */
    @PreAuthorize("@ss.hasPermi('villagers:house:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody VigHouse house)
    {

        house.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iVigHouseService.updateHouse(house));
    }

    /**
     * 根据建房编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('villagers:house:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable Long houseId)
    {
        return AjaxResult.success(iVigHouseService.selectHouseById(houseId));
    }

    /**
     * 删除建房
     */
    @PreAuthorize("@ss.hasPermi('villagers:house:remove')")
    @Log(title = "建房管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{houseIds}")
    public AjaxResult remove(@PathVariable Long[] houseIds)
    {
        return toAjax(iVigHouseService.deleteHouseByIds(houseIds));
    }

    /**
     * 获取建房选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<VigHouse> houses = iVigHouseService.selectHouseAll();
        return AjaxResult.success(houses);
    }


    /**
     * 提交申请
     */
    @PreAuthorize("@ss.hasPermi('villagers:house:pass')")
    @PutMapping("/review/{houseIds}")
    public AjaxResult pass(@PathVariable Long[] houseIds){

        return toAjax(iVigHouseService.passHouses(houseIds));
    }



}
